﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Collections;
using System.Web.UI;
using System.Web.Security;
using SubSonic;
using System.Configuration;
using xtra.domain.Subsonic3;
using SubSonic.Repository;

namespace sb.core.Framework {
    public interface ISiteContext {
        BasePage BasePage { get; set; }
        BaseMaster BaseMaster { get; set; }
        IRepository Repo { get; set; }
        aspnet_Application Application { get; set; }
        Language Language { get; set; }
        WebPage Page { get; set; }
        Plugins_In_Page RootModule { get; set; }
        void Set();
    }
    public class SiteContextInstance : ISiteContext {
        #region Fields
        private readonly HttpContext context;
        private HybridDictionary items;
        #endregion

        #region Properties
        public BasePage BasePage { get; set; }
        public BaseMaster BaseMaster { get; set; }
        public aspnet_Application Application { get; set; }
        public Language Language { get; set; }
        public WebPage Page { get; set; }
        public Plugins_In_Page RootModule { get; set; }

        public IRepository Repo { get; set; }

        public HttpContext Context { get { return context; } }
        public IDictionary Items {
            get { return items ?? (items = new HybridDictionary()); }
        }

        public MembershipUser User {
            get {
                MembershipUser ret = null;
                if (Context.User.Identity.IsAuthenticated) {
                    ret = Membership.GetUser();
                }
                return ret;
            }
        }
        public List<string> UserRoles {
            get {
                List<string> ret = null;
                if (Context.User.Identity.IsAuthenticated) {
                    ret = new List<string>(Roles.GetRolesForUser());
                }
                return ret;
            }
        }

        #endregion

        #region .ctor
        internal SiteContextInstance(HttpContext context) {
            Repo = new SimpleRepository(SimpleRepositoryOptions.None);
            this.context = context;
        }
        #endregion

        private bool IsDefaultPage() {
            return Context.Request.Path.ToLower().Remove(0, 1) == ConfigurationManager.AppSettings["defaultPageName"];
        }

        #region ISiteContext Members

        public void Set() {
            SetPage();
            BasePage = (BasePage)Context.CurrentHandler;
            BaseMaster = (BaseMaster) BasePage.Master;
        }

        public void SetPage() {
            string uri = RewriteContext.Current.Params[GlobalSet.QUERY_PARAM_PAGE_URI];
            if (uri == null) {
                uri = ConfigurationManager.AppSettings["homepageURI"];
            }
            try {
                Page = Repo.Single<WebPage>(a => a.URI == uri);
                var coreStructure = Repo.Single<Domain>(Page.DomainsId);
                Language = Repo.Single<Language>(coreStructure.LanguageId);
                Application = Repo.Single<aspnet_Application>(coreStructure.ApplicationId);

                var t = (from a in Repo.All<Plugins_In_Page>()
                         where a.ParentId == null && a.RootPageId == Page.Id
                         select a).ToList();
                if (t != null && t.Count() > 0) {
                    RootModule = t[0];
                }

            } catch (Exception ex) {
                throw ex; //Exception("Page not found. Must be implemented 404 redirect");
            }
        }

        private void SetApplication() {
        }
        private void SetLocalization() {
        }

        #endregion
    }
}
